function httpRequest(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      callback(xhr.responseText);
    }
  }
  xhr.send();
}

function showWeather(result) {
  result = JSON.parse(result);
  if (result.status !== 1000) {
    document.getElementById('weather').innerHTML = result.desc;
    return;
  }
  var list = result.data.forecast;
  var html = `<p>当前城市：${result.data.city}</p><table><tr><th>日期</th><th>天气</th><th>最低温度</th><th>最高温度</th></tr>`;
  for (var i in list) {
    html += '<tr>';
    html += '<td>' + list[i].date + '</td>';
    html += '<td>' + list[i].type + '</td>';
    html += '<td>' + (list[i].low.split(' ')[1]) + ' </td>';
    html += '<td>' + (list[i].high.split(' ')[1]) + ' </td>';
    html += '</tr>';
  }
  html += '</table>';
  document.getElementById('weather').innerHTML = html;
}

var city = localStorage.city;
city = city ? city : '北京';
var url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + city;
httpRequest(url, showWeather);